package com.lijie.ljblog.controller;

import com.lijie.ljblog.service.UtilService;
import com.lijie.ljblog.util.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Description 共性接口
 * @Author lijie
 * @Date 2022/4/15 11:20
 **/
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {
    @Value("${web.blogImg-path}")
    private String imagePath;
    @Value("${web.music-path}")
    private String musicPath;
    @Value("${web.headImg-path}")
    private String headImgPath;
    @Value("${fileUploadUrl}")
    private String fileUploadUrl;
    @Resource
    UtilService utilService;

    @RequestMapping("/imgUpload")
    public Result<String> imgUpload(MultipartFile image, HttpServletRequest request){
        Result<String> res=new Result<>();
        try {
            String s = utilService.fileUpload(image, imagePath);
            res.setSuccess(fileUploadUrl+s);
        } catch (IOException e) {

            res.setErrored(e.getMessage());
        }
        return res;
    }
    @RequestMapping("/musicUploadList")
    public Result<String> imgUpload(MultipartHttpServletRequest mulRequest, HttpServletRequest request) throws Exception {
        Result<String> res=new Result<>();
        utilService.musicUploadList(mulRequest, musicPath);
        return res;
    }

    @ApiOperation("新增网站浏览量")
    @GetMapping("/AddWebSiteVisits")
    public Result<Long> webSiteVisits(HttpServletRequest request){
        return utilService.webSiteVisits(request);
    }




}
